package ua.edu.nuos.jeetraining2012.cms.util.template.homework;

import org.slf4j.Logger;
import ua.edu.nuos.jeetraining2012.cms.dto.HomeWorkDetailedDTO;
import ua.edu.nuos.jeetraining2012.cms.entity.StudentsToHomeWorkEntity;

import java.util.Date;
import java.util.Map;

/**
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 26.06.13 Time: 14:37
 * @since 1.0
 */
public class ViewTemplate extends DefaultHomeWorkTemplate {

    /** Define section title. */
    private String sectionTitle = "section title";

    /** Define material title. */
    private String title;

    /** Define material start date. */
    private Date materialStartDate;

    /** Define material finish date. */
    private Date materialFinishDate;

    /** Define homework ID. */
    private Long homeWorkId;

    /** Define homework description. */
    private String homeWorkDescription;

    /** Define attachment */
    private String fileUrl;

    /** Define homework start date. */
    private Date homeWorkStartDate;

    /** Define current collapse num for accordion element. */
    private int currentCollapseNum;

    /** Define logger */
    final private static Logger logger = org.slf4j.LoggerFactory.getLogger(ViewTemplate.class);

    // Constructors.
    public ViewTemplate() {
        super();
        logger.debug("ViewTemplate invocation.");
        templateName = "/view/homework/view.ftl";
        logger.debug("redefine templateName to {}", templateName);
    }

    public ViewTemplate(Long homeworkId) {
        this();
        setHomeWorkId(homeworkId);
        logger.debug("Homework ID {}. Trying to init values...", homeworkId);
        initValues();
    }

    // Getters & Setters.
    public String getSectionTitle() {
        return sectionTitle;
    }

    public void setSectionTitle(String sectionTitle) {
        this.sectionTitle = sectionTitle;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getMaterialStartDate() {
        return materialStartDate;
    }

    public void setMaterialStartDate(Date materialStartDate) {
        this.materialStartDate = materialStartDate;
    }

    public Date getMaterialFinishDate() {
        return materialFinishDate;
    }

    public void setMaterialFinishDate(Date materialFinishDate) {
        this.materialFinishDate = materialFinishDate;
    }

    public Long getHomeWorkId() {
        return homeWorkId;
    }

    public void setHomeWorkId(Long homeWorkId) {
        this.homeWorkId = homeWorkId;
    }

    public String getHomeWorkDescription() {
        return homeWorkDescription;
    }

    public void setHomeWorkDescription(String homeWorkDescription) {
        this.homeWorkDescription = homeWorkDescription;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public Date getHomeWorkStartDate() {
        return homeWorkStartDate;
    }

    public void setHomeWorkStartDate(Date homeWorkStartDate) {
        this.homeWorkStartDate = homeWorkStartDate;
    }

    public int getCurrentCollapseNum() {
        return currentCollapseNum;
    }

    public void setCurrentCollapseNum(int currentCollapseNum) {
        this.currentCollapseNum = currentCollapseNum;
    }

// custom methods.
    /** @inheritDoc */
    @Override
    @SuppressWarnings("unchecked")
    public Map<String, Object> getMap() {
        logger.debug("getMap() - started.");

        Map<String, Object> map = super.getMap();
        Map<String, Object> i18nMap = (Map<String, Object>) map.get("i18n");
        map.put("i18n", getI18nMap(i18nMap));
        map.put("sectionTitle", sectionTitle);
        map.put("title", title);
        map.put("materialStartDate", materialStartDate);
        map.put("materialFinishDate", materialFinishDate);
        map.put("homeWorkDescription", homeWorkDescription);
        map.put("materials_titles", materialTitles);
        map.put("fileUrl", fileUrl);
        map.put("homeWorkStartDate", homeWorkStartDate);
        map.put("homeWorkFinishDate", homeWorkFinishDate);
        map.put("isExpiredHomeWork", new Date().after(homeWorkFinishDate));
        map.put("homeWorkMaxScore", homeWorkMaxScore);
        map.put("isDone", isDone);
        map.put("studentScore", studentScore);
        map.put("courseTitle", courseTitle);
        map.put("courseStartDate", courseStartDate);
        map.put("courseFinishDate", courseFinishDate);
        map.put("courseId", courseId);
        map.put("syllabusLink", "#");
        map.put("hwList", hwList);
        map.put("isEnrolled", true);
        map.put("homeworkId", homeWorkId);
        map.put("currentCollapseNum", currentCollapseNum);

        logger.debug("Template Map {}", map);
        logger.debug("getMap() - finished.");
        return map;
    }

    /**
     * Wrapper for setUp methods.
     */
    private void initValues() {
        logger.debug("initValues() - started.");

        if (userEntity != null && homeWorkId != null) {
            logger.debug("Getting info about homework.");
            setUpHomeWorkInfo();

            logger.debug("Getting info about student progress");
            setUpStudentProgress();
        } else {
            logger.error("Got illegal value in list: userEntity {}, homeWorkId {}", userEntity, homeWorkId);
        }

        logger.debug("initValues() - finished.");
    }

    /**
     * Collect and fill out fields with homework information.
     */
    private void setUpHomeWorkInfo() {
        logger.debug("setUpHomeWorkInfo() - started.");

        HomeWorkDetailedDTO dto = homeWorkService.getHomeWork(homeWorkId);

        if (dto != null) {
            homeWorkDescription = dto.getHomeWorkDescription();
            fileUrl = dto.getFileUrl();
            homeWorkStartDate = dto.getHomeWorkStartDate();
            homeWorkFinishDate = dto.getHomeWorkFinishDate();
            homeWorkMaxScore = dto.getHomeWorkMaxScores();
            courseId = dto.getCourseId();
            fetchHWList(courseId);
            fetchCommonCourseInfo(courseId);
            fetchCourseTitles();
        } else {
            homeWorkDescription = new String();
            fileUrl = new String();
            homeWorkStartDate = new Date();
            homeWorkFinishDate = new Date();
        }
        logger.info("Got DTO {}", dto);

        logger.debug("setUpHomeWorkInfo() - finished.");
    }

    /**
     * Collect students progress within homework and fill out necessary fields.
     */
    private void setUpStudentProgress() {
        logger.debug("setUpStudentProgress() - started.");

        StudentsToHomeWorkEntity entity = homeWorkService.getStudentProgress(userEntity.getId(), homeWorkId);

        if (entity != null) {
            setStudentScore(entity.getSroces());
            setDone(entity.isDone());
        }

        logger.debug("setUpStudentProgress() - finished.");
    }
}
